/**
 * 
 */

package org.xserver.core.command;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author jkonopka
 * 
 */
public class CompositeCommand implements ICommand {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2490360856716533720L;
	private final List<ICommand> delegates;

	/**
	 * Create a new instance of {@link CompositeCommand}.<br>
	 * Delegates a new empty {@link ArrayList} with {@link ICommand} types.<br>
	 */
	public CompositeCommand() {
		this(new ArrayList<ICommand>());
	}

	/**
	 * @param delegates List with {@link ICommand} delegates
	 */
	public CompositeCommand(final ICommand... delegates) {
		this(Arrays.asList(delegates));
	}

	/**
	 * @param delegates List with {@link ICommand} delegates
	 */
	public CompositeCommand(final List<ICommand> delegates) {
		this.delegates = delegates;
	}

	/**
	 * Add the delegate {@link ICommand} to the executable list with delegates.<br>
	 * 
	 * @param delegate {@link ICommand}
	 */
	public void add(final ICommand delegate) {
		this.getDelegates().add(delegate);
	}

	/**
	 * @param delegates List with {@link ICommand} delegates
	 */
	public void add(final List<ICommand> delegates) {
		this.getDelegates().addAll(delegates);
	}

	/**
	 * @param delegates List with {@link ICommand} delegates
	 */
	public void add(final ICommand... delegates) {
		this.add(Arrays.asList(delegates));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.commons.command.ICommand#execute()
	 */
	@Override
	public void execute() throws Exception {
		for (final ICommand delegate : getDelegates()) {
			delegate.execute();
		}
	}

	/**
	 * @return the delegates
	 */
	public List<ICommand> getDelegates() {
		return delegates;
	}

}
